home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Re: [Q] WEIRD macro bug only when compiling with GCC
- Date: 22 Jan 1996 04:29:16 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan21212916@qcd.lanl.gov>
- References: <DLKABL.KGK@info.physics.utoronto.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: olivers@helios.physics.utoronto.ca's message of Mon, 22 Jan 1996 02:45:21 GMT
-
- --text follows this line--
- In article <DLKABL.KGK@info.physics.utoronto.ca>
- olivers@helios.physics.utoronto.ca (Oliver Schonborn) writes:
- <snip>
- The macro *works FINE* when I compile with our HP compiler,
- cc: it prints out one hundred different numbers. BUT when
- I use gcc to compile, instead what it prints is double the
- result of the first SQR on the line, ie SQR( i*dx, f) * 2.
- Why isn't (a=b, b*b)+(a=c, c*c) equal to b*b+c*c for GCC?
-
- Because it invokes undefined behaviour: which means that you are doing
- something which the compiler need not anticipate ... and so anything
- could happen. There is a rule in C which says that the same object
- cannot be modified more than once without intervening sequence
- points. In your code fragment `a' is modified twice without
- intervening sequence point.
-
- The issue is slightly confusing because the comma does enforce a
- sequence point between the previous and the next expressions:
- i.e. there is a sequence point between a=b and b*b; likewise between
- a=c and c*c. However as the + operator does not enforce _any_ order
- (not even the existence of an order) between its two operands, there
- is no sequence point _between_ a=b and a=c.
-
- Actually your situation is still more undefined (if such a term be
- possible). You are talking about (a=b,a*a)+(a=c,a*a). This has the
- problem mentioned above, but in addition there is no sequence point
- between the a=c and the first a*a (likewise between the a=b and the
- second a*a). Thus a is both modified and independently (i.e. not as
- part of a process to ascertain the modified value or the value of the
- expression doing the modification) accessed without an intervening
- sequence point: this too is undefined by the same rule.
-
- Although I've decided to implememt the macro by a function
- instead (float sqr(float x) {return x*x;}), I'm curious to
- know what is wrong with the macro I wrote. Anyone have a
- clue?
-
- Just change the second f to a g (another float) and you get a code
- with defined behaviour.
-
- Thanks for posting a real life example of this error.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-